package com.taijihuabao.common.activititasklistener.leavelistener;

import com.taijihuabao.modules.entity.ApprovalEntity;
import com.taijihuabao.modules.entity.ApprovalHistoryEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.mapper.ApprovalHistoryMapper;
import com.taijihuabao.modules.mapper.ApprovalMapper;
import com.taijihuabao.modules.mapper.DeptUserMapper;
import com.taijihuabao.modules.model.approval.vo.ApprovalVO;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 提交请假申请
 * @author xiaob
 */
@Component
public class ApprovalTaskListener implements TaskListener {

    @Resource
    private ApprovalMapper approvalMapper;
    @Resource
    private ApprovalHistoryMapper approvalHistoryMapper;
    @Resource
    private DeptUserMapper deptUserMapper;

    @Override
    public void notify(DelegateTask delegateTask) {
        //审批id
        String businessKey = delegateTask.getExecution().getProcessInstanceBusinessKey();
        //查询审批单
        ApprovalEntity approvalEntity = approvalMapper.selectById(Long.valueOf(businessKey));
        //历史表对象
        ApprovalHistoryEntity approvalHistoryEntity = new ApprovalHistoryEntity();
        if (!Objects.isNull(approvalEntity)) {
            //设置申请人（修改提交申请）
            delegateTask.setAssignee(String.valueOf(approvalEntity.getApplyUserId()));
            //获取审批用户id
            approvalHistoryEntity.setApprovalUserId(Long.valueOf(delegateTask.getAssignee()));
            approvalEntity.setApprovalUserId(approvalHistoryEntity.getApprovalUserId());
            //查询审批用户的部门
            List<DeptUserEntity> deptUserEntities = deptUserMapper
                    .selectDeptByUserId(Long.valueOf(delegateTask.getAssignee()));
            approvalHistoryEntity.setApprovalDeptCode(deptUserEntities.get(0).getDeptCode());
            approvalEntity.setApprovalDeptCode(approvalHistoryEntity.getApprovalDeptCode());
        }
        approvalHistoryEntity.setApplyDeptCode(approvalEntity.getApplyDeptCode());
        approvalHistoryEntity.setApplyName(approvalEntity.getApplyName());
        approvalHistoryEntity.setApplyUserId(approvalEntity.getApplyUserId());
        approvalHistoryEntity.setApprovalCode(approvalEntity.getApprovalCode());
        approvalHistoryEntity.setApprovalFlow(delegateTask.getName());
        //判断是否是重新提交
        List<ApprovalVO> approvals = approvalHistoryMapper.selectByApprovalCode(approvalEntity.getApprovalCode());
        for(ApprovalVO approvalVO : approvals){
            String flow = "提交申请";
            if(flow.equals(approvalVO.getApprovalFlow())){
                if(flow.equals(approvalHistoryEntity.getApprovalFlow())){
                    approvalHistoryEntity.setApprovalFlow("重新提交");
                }
            }
        }
        approvalHistoryEntity.setApprovalOpinion(null);
        approvalHistoryEntity.setApprovalState("ing");
        approvalHistoryEntity.setApprovalType(approvalEntity.getApprovalType());
        approvalHistoryEntity.setApprovalTask(delegateTask.getId());
        approvalEntity.setApprovalTask(approvalHistoryEntity.getApprovalTask());
        //新增工作流历史记录
        approvalHistoryMapper.insertApproval(approvalHistoryEntity);
        //修改工作流记录
        approvalMapper.updateApproval(approvalEntity);
    }

}
